package icdecs.dao;

import icdecs.connection.ConnectDatabase;
import icdecs.model.DataModel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class GetListDAO {

	public boolean InsertData(List<DataModel> listData) throws SQLException {
		Connection con = null;
		PreparedStatement pst = null;

		String sql = "Insert into tg_list_tbl(auth_id, org_id, card_id, generated_time) values(?,?,?,?)";
		try {
			con = ConnectDatabase.getConnection();
			pst = con.prepareStatement(sql);

			con.setAutoCommit(false);

			for (DataModel dataModel : listData) {
				pst.setString(1, dataModel.getAuthenticationId());
				pst.setString(2, dataModel.getOrganizationId());
				pst.setString(3, dataModel.getCardId());
				pst.setString(4, dataModel.getGenerated_time());
				pst.addBatch();
			}

			pst.executeBatch();
			con.commit();
			System.out.println("Insert all records success!");
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			con.rollback();
		} finally {
			if (con != null)
				con.close();
			if (pst != null) {
				pst.close();
			}
		}
		return false;
	}
}
